var utils = require("../utils/utils");
var ocr_utils = require("../utils/ocr_utils");

var AD_CONST_MIN_INTERVAL = 40;
var AD_CONST_MAX_GO_BACK_TIMES = 4;

var ad = {
  in_task: false,
  in_home: false,
  if_current_ad_is_over: null, //判断广告实际是否比预计时间提前结束
  if_in_home: null, //判断是否在首页
  if_in_task: null, //判断是否是在任务页
  has_next_contents: [], //判断是否有下一个广告
  init(if_current_ad_is_over, if_in_task, if_in_home, has_next_contents) {
    console.show();
    console.log("ad init");
    this.if_current_ad_is_over =
      if_current_ad_is_over ||
      function () {
        var over_contents = ["领取成功"];
        for (var i = 0; i < over_contents.length; i++) {
          var over_content = over_contents[i];
          if (utils.exists_text_starts_with(over_content)) {
            console.log("ad init: 找到--" + over_content + "--");
            return true;
          }
        }
        return false;
      };
    this.if_in_task =
      if_in_task ||
      function () {
        var in_task_contents = ["立即收下", "开心收下"];
        for (var i = 0; i < in_task_contents.length; i++) {
          var in_task_content = in_task_contents[i];
          if (utils.exists_text_starts_with(in_task_content)) {
            console.log("ad init: 找到--" + in_task_content + "--");
            return true;
          }
        }
        return ocr_utils.screen_exists_text_all(["赚钱任务"], false);
      };
    this.if_in_home =
      if_in_home ||
      function () {
        return ocr_utils.screen_exists_text_all(["首页"], false);
      };
    this.has_next_contents = has_next_contents || ["领取奖励"];
    //开始看广告
    var ad_interval = AD_CONST_MIN_INTERVAL + random(0, 5);
    console.log("ad init: 即将开始看广告，预计时间" + ad_interval + "s");
    this.run(ad_interval);
  },
  run(ad_interval) {
    console.log("ad run: " + ad_interval + "秒");
    var if_current_ad_is_over_interval = 2; //每隔2s检查当前广告是否已经结束
    var is_over_early = false;
    var has_watch_interval = 0;
    var count = 0;
    while (ad_interval > if_current_ad_is_over_interval) {
      if (has_watch_interval > 30) {
        if (!utils.exists_text_starts_with("反馈")) {
          console.log(
            "ad run: 已过--" +
              has_watch_interval +
              "--s但是没找到--反馈--,即将执行一次back()"
          );
          back();
          sleep(2 * 1000);
        }
      }
      if (this.if_current_ad_is_over && this.if_current_ad_is_over()) {
        var before_over_interval = random(1, 3);
        console.log(
          "ad run: 广告已结束,再过" + before_over_interval + "s后停止当前广告"
        );
        sleep(before_over_interval * 1000);
        is_over_early = true; //广告实际比预计的时间提前结束
        break;
      }
      sleep(if_current_ad_is_over_interval * 1000);
      ad_interval = ad_interval - if_current_ad_is_over_interval;
      has_watch_interval = has_watch_interval + if_current_ad_is_over_interval;
      count = count + 1;
      if (count % 5 == 0) {
        console.log("ad run: 广告时间预计还剩" + ad_interval + "秒");
      }
    }
    if (is_over_early) {
      this.go_back();
      return;
    }
    if (ad_interval > 0) {
      console.log("ad run: 广告时间还剩最后的" + ad_interval + "秒");
      sleep(ad_interval * 1000);
      this.go_back();
      return;
    }
  },
  go_back() {
    console.log("ad go_back");
    var index = 0;
    while (index < AD_CONST_MAX_GO_BACK_TIMES) {
      back();
      sleep(2 * 1000);
      for (var i = 0; i < this.has_next_contents.length; i++) {
        var content = this.has_next_contents[i];
        if (utils.exists_text_starts_with(content)) {
          console.log("ad go_back: 找到--" + content + "--");
          utils.click_text_starts_with(content);
          sleep(2 * 1000);
          //开始看下一次的广告
          var ad_interval = AD_CONST_MIN_INTERVAL + random(0, 5);
          console.log(
            "ad go_back: 即将开始看广告，预计时间" + ad_interval + "s"
          );
          this.run(ad_interval);
          return;
        }
      }
      if (this.if_in_task && this.if_in_task()) {
        console.log("ad go_back: 判断在任务页");
        this.in_task = true;
        this.after();
        return;
      }
      if (this.if_in_home && this.if_in_home()) {
        console.log("ad go_back: 判断在app首页");
        this.in_home = true;
        this.after();
        return;
      }
      console.log("ad go_back: 第" + (index + 1) + "次执行back");
      index = index + 1;
    }
    this.after();
  },
  after() {
    console.log("ad after");
    this.next();
  },
  next() {
    console.log("ad next");
    console.hide();
  },
};

module.exports = ad;
